package com.order.model;  

import java.util.List;

import com.jfinal.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/** 
 * @Title: OrderLine.java 
 * @Package com.order.model 
 * @Description: 订单行
 * @author LinBo
 * @date 2014-3-25 下午11:53:24 
 */

@TableBind(tableName="order_line", pkName="id")
public class OrderLine extends Model<OrderLine> {

	private static final long serialVersionUID = -2414161435070580690L;
	public static final OrderLine dao = new OrderLine();
	public static final String ID ="id";
	public static final String ORDER_HEAD_ID = "order_head_id";
	public static final String GRADE = "grade";
	public static final String EVALUATION = "evaluation";
	public static final String CUISINE_DISH_ID = "cuisine_dish_id";
	public static final String QUANTITY = "quantity";
	public static final String STATUS = "status";
	protected static final String TOTAL = "total";
	
	
	/** 
	* @Title: getDetialById 
	* @Description: 由订单头id获取详情
	* @param orderHeadId
	* @return   
	*/ 
	public String getDetialById(String orderHeadId) {
		String select = "select ol.*, c.name cuisine_name, d.name dish_name, d.price "
				+ " from order_line ol left join cuisine_dish cd "
				+ " 	on ol.cuisine_dish_id = cd.id "
				+ " left join cuisine c "
				+ "		on c.id = cd.cuisine_id "
				+ " left join dish d "
				+ "		on d.id = cd.dish_id "
				+ " where ol.order_head_id= ?";
		List<OrderLine> orderLines = dao.find(select, orderHeadId);
		StringBuffer json = new StringBuffer("");
		if(orderLines != null && !orderLines.isEmpty()){
			json.append("{\"order_head_id\":\"" + orderHeadId + "\",");
			json.append("\"items\":[");
			//遍历所有订单行
			for(OrderLine line : orderLines){
				json.append("{\"order_line_id\":\"" + line.get(ID, "") + "\",");
				json.append("\"grade\":\"" + line.get(GRADE, "") + "\",");
				json.append("\"price\":\"" + line.get("price", "") + "\",");
				json.append("\"evaluation\":\"" + line.get(EVALUATION, "") + "\",");
				json.append("\"cuisine_dish_id\":\"" + line.get(CUISINE_DISH_ID, "") + "\",");
				json.append("\"cuisine_name\":\"" + line.get("cuisine_name", "") + "\",");
				json.append("\"dish_name\":\"" + line.get("dish_name", "") + "\",");
				json.append("\"quantity\":\"" + line.get(QUANTITY, "") + "\",");
				json.append("\"status\":\"" + line.get(STATUS, "") + "\"},");
			}
			json.deleteCharAt(json.length() - 1);
			json.append("]}");
		}
		return json.toString();
	}


	/** 
	* @Title: getEvaluation 
	* @描述: 获取菜品评价信息
	* @param cuisineDishId
	* @return   
	*/ 
	public Page<OrderLine> getEvaluation(int page, int pageSize, String cuisineDishId) {
		String select = " select ol.id, ol.grade, ol.evaluation, ol.quantity, a.name, oh.create_date ";
		String sqlExceptSelect = " from order_line ol, order_head oh, account a "
				+ " where ol.cuisine_dish_id = ?  "
				+ " and ol.status = '4' "
				+ " and oh.id = ol.order_head_id "
				+ " and oh.account_id = a.id ";
		Page<OrderLine> list = dao.paginate(page, pageSize, select, sqlExceptSelect, cuisineDishId);
		return list;
	}
	
	/** 
	* @Title: getAllOrder 
	* @描述: 获取客户所有订单
	* @return   
	*/ 
	public List<Record> getAllLine(String headId) {
		String sql = " select ol.id,  ol.cuisine_dish_id, ol.quantity, "
				+ " ol.grade, ol.evaluation, d.name dish_name, "
				+ " d.image, ol.total sum, ol.status "
				+ " from order_line ol, cuisine_dish cd, dish d "
				+ " where ol.cuisine_dish_id = cd.id "
				+ " and cd.dish_id = d.id "
				+ " and ol.order_head_id = ? ";
		List<Record> list = Db.find(sql, headId);
		return list;
	}


	/** 
	* @Title: doEvaluation 
	* @描述: 菜品评价
	* @param orderLineId
	* @param evaluation2
	* @param grade2   
	*/ 
	public void doEvaluation(String orderLineId, String evaluation,	String grade) {
		dao.findById(orderLineId)
			.set(OrderLine.EVALUATION, evaluation)
			.set(OrderLine.GRADE, grade)
			.set(OrderLine.STATUS, OrderHead.STATUS_FINSH)
			.update();
		
	}
}
  